package com.jade.shoesmallinterface.service.impl;

import com.jade.shoesmallinterface.entity.User;
import com.jade.shoesmallinterface.mapper.UserMapper;
import com.jade.shoesmallinterface.service.UserService;
import com.jade.shoesmallinterface.utils.Md5Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    private static final Logger logger = LoggerFactory.getLogger(UserService.class);
    @Override
    public List<User> selectAllUsers() {
        List<User> users = userMapper.selectAllUsers();
//        List<UserVo> userVos =new  ArrayList<UserVo>();
//
//        users.forEach(item ->{
//            UserVo userVo= new UserVo();
//            userVo.setUsername(item.getUsername());
//            userVo.setAge(item.getAge()==0?"男":"女");
//            userVo.setUsername(item.getUsername());
//            userVo.setUsername(item.getUsername());
//            userVo.setUsername(item.getUsername());
//            userVo.setUsername(item.getUsername());
//            userVos.add(userVo);
//        });
        return users;
    }

    @Override
    public int insertUserInfo(User user) {
        // 对密码进行加密
        String encryptedPassword = Md5Util.getMD5String(user.getPassword());
        user.setPassword(encryptedPassword);
        return userMapper.insertUserInfo(user);
    }
    @Override
    public int deleteUserByName(String username) {
        return userMapper.deleteUserByName(username);
    }

    @Override
    public int updateUserInfo(User user) {
        // 对密码进行加密
        String encryptedPassword = Md5Util.getMD5String(user.getPassword());
        user.setPassword(encryptedPassword);
        return userMapper.updateUserInfo(user);
    }

    @Override
    public User selectUserInfoByEmail(String email) {
        try{
            return userMapper.selectUserInfoByEmail(email);
        }catch (NullPointerException e){
            logger.warn(e.getCause().getMessage());
            return  null;
        }

    }
}
